###########################
### POPULIST GOVERNMENT SUPPORT AND FRONTLINE WORKERS' SELF-EFFICACY DURING CRISIS
### GOVERNANCE
### REPLICATION FILE OF THE EMPIRICAL ANALYSIS
### MICHELLE FERNANDEZ, GABRIELA LOTTA, EVA THOMANN, JAN VOGLER, ARTHUR LEANDRO, & MARCELA CORRÊA
###########################

###########################
### INSTALL REQUIRED PACKAGES
###########################

install.packages("ggplot2")
install.packages("scales")
install.packages("gridExtra")



###########################
### LOAD DATASET
###########################

data_flw=read.csv("frontlineworkers_data.csv", stringsAsFactors=F)



###########################
### TABLES IN THE ARTICLE AND ONLINE APPENDICES
###########################

###########################
### DESCRIPTIVE STATISTICS
### TABLE 2 IN THE ARTICLE
###########################

### ALL RELEVANT VALUES CAN BE EXTRACTED VIA THE FOLLOWING COMMAND

summary(data_flw)



###########################
### KEY EXPLANATIONS FOR SELF-EFFICACY
### TABLE 3 IN THE ARTICLE
###########################

logit_main_models_01=glm(selfefficacy ~ fed_gov_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_main_models_01)

logit_main_models_02=glm(selfefficacy ~ fed_gov_support + state_gov_support + local_gov_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_main_models_02)

logit_main_models_03=glm(selfefficacy ~ resources + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_main_models_03)

logit_main_models_04=glm(selfefficacy ~ managerial_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_main_models_04)

logit_main_models_05=glm(selfefficacy ~ demands + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_main_models_05)

logit_main_models_06=glm(selfefficacy ~ fed_gov_support + state_gov_support + local_gov_support + resources + managerial_support + demands + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_main_models_06)



###########################
### INTERACTION MODELS
### TABLE A3 IN THE ONLINE APPENDICES
###########################

logit_int_models_01=glm(selfefficacy ~ fed_gov_support + resources + int_fed_resources + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_01)

logit_int_models_02=glm(selfefficacy ~ fed_gov_support + managerial_support + int_fed_managerial + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_02)

logit_int_models_03=glm(selfefficacy ~ state_gov_support + resources + int_state_resources + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_03)

logit_int_models_04=glm(selfefficacy ~ state_gov_support + managerial_support + int_state_managerial + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_04)

logit_int_models_05=glm(selfefficacy ~ local_gov_support + resources + int_local_resources + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_05)

logit_int_models_06=glm(selfefficacy ~ local_gov_support + managerial_support + int_local_managerial + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_06)

logit_int_models_07=glm(selfefficacy ~ fed_gov_support + state_gov_support + local_gov_support + resources + managerial_support + int_fed_resources + int_fed_managerial + int_state_resources + int_state_managerial + int_local_resources + int_local_managerial + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_int_models_07)



###########################
### CORRELATION OF GOVERNMENT SUPPORT WITH MANAGERIAL SUPPORT
### TABLE A4 IN THE ONLINE APPENDICES
###########################

lm_corr_gov_man_support=lm(managerial_support ~ fed_gov_support + state_gov_support + local_gov_support, data=data_flw)
summary(lm_corr_gov_man_support)



###########################
### ADDITIONAL MODELS (ALTERNATIVE COMBINATIONS OF INDEPENDENT VARIABLES)
### TABLE A5 IN THE ONLINE APPENDICES
###########################

logit_add_models_01=glm(selfefficacy ~ fed_gov_support + managerial_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_01)

logit_add_models_02=glm(selfefficacy ~ fed_gov_support + state_gov_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_02)

logit_add_models_03=glm(selfefficacy ~ fed_gov_support + state_gov_support + managerial_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_03)

logit_add_models_04=glm(selfefficacy ~ fed_gov_support + local_gov_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_04) 

logit_add_models_05=glm(selfefficacy ~ fed_gov_support + local_gov_support + managerial_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_05)

logit_add_models_06=glm(selfefficacy ~ fed_gov_support + state_gov_support + local_gov_support + managerial_support + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_06)

logit_add_models_07=glm(selfefficacy ~ fed_gov_support + state_gov_support + local_gov_support + resources + demands + CHW + HW + female + white + age, family = "binomial"(link = logit), data=data_flw)
summary(logit_add_models_07)



###########################
### FIGURES IN THE ARTICLE
###########################

###########################
### DESCRIPTIVE VISUALIZATION OF KEY VARIABLES
### FIGURE 2 IN THE ARTICLE
###########################

### PREPARE DATA FRAME

var.names=c(rep("Self-Efficacy",3229),rep("Govt. Supp. (Fed.)",3229),rep("Govt. Supp. (State)",3229),rep("Govt. Supp. (Local)",3229),rep("Resources",3229),rep("Demands",3229),rep("Manag. Supp.",3229))
var.values=c(data_flw$selfefficacy,data_flw$fed_gov_support,data_flw$state_gov_support,data_flw$local_gov_support,data_flw$resources,data_flw$demands,data_flw$managerial_support)

var.values=as.factor(var.values)

data_visual=data.frame(var.names,var.values)

### CREATE THE PLOT

library(ggplot2)

descriptive_plot=ggplot(data_visual, aes(x = var.names, fill = var.values)) + 
  geom_bar(position = "fill") +
  labs(y = "Proportion") + scale_fill_grey(start=0.9,end=0.1,labels=c('0','0.33', '0.5',"0.67","1")) + theme_bw() + xlab("Variable Names")  + guides(fill=guide_legend(title="Values")) + theme(axis.text=element_text(size=10,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5))

descriptive_plot



###########################
### CREATE FIGURE: SELF-EFFICACY AND GOVERNMENT SUPPORT
###########################

###########################
### NEW DATA FRAMES
###########################

### FEDERAL GOVERNMENT SUPPORT

nd1 = data.frame(fed_gov_support=c(0,1), state_gov_support=rep(mean(data_flw$state_gov_support, na.rm=T),2), local_gov_support=rep(mean(data_flw$local_gov_support, na.rm=T),2), resources=rep(mean(data_flw$resources, na.rm=T),2), managerial_support=rep(mean(data_flw$managerial_support, na.rm=T),2), demands=rep(mean(data_flw$demands, na.rm=T),2), CHW=rep(mean(data_flw$CHW, na.rm=T),2), HW=rep(mean(data_flw$HW, na.rm=T),2), female=rep(mean(data_flw$female, na.rm=T),2), white=rep(mean(data_flw$white, na.rm=T),2), age=rep(mean(data_flw$age, na.rm=T),2))
nd1

### STATE GOVERNMENT SUPPORT

nd2 = data.frame(fed_gov_support=rep(mean(data_flw$fed_gov_support, na.rm=T),2), state_gov_support=c(0,1), local_gov_support=rep(mean(data_flw$local_gov_support, na.rm=T),2), resources=rep(mean(data_flw$resources, na.rm=T),2), managerial_support=rep(mean(data_flw$managerial_support, na.rm=T),2), demands=rep(mean(data_flw$demands, na.rm=T),2), CHW=rep(mean(data_flw$CHW, na.rm=T),2), HW=rep(mean(data_flw$HW, na.rm=T),2), female=rep(mean(data_flw$female, na.rm=T),2), white=rep(mean(data_flw$white, na.rm=T),2), age=rep(mean(data_flw$age, na.rm=T),2))
nd2

### LOCAL GOVERNMENT SUPPORT

nd3 = data.frame(fed_gov_support=rep(mean(data_flw$fed_gov_support, na.rm=T),2), state_gov_support=rep(mean(data_flw$state_gov_support, na.rm=T),2), local_gov_support=c(0,1), resources=rep(mean(data_flw$resources, na.rm=T),2), managerial_support=rep(mean(data_flw$managerial_support, na.rm=T),2), demands=rep(mean(data_flw$demands, na.rm=T),2), CHW=rep(mean(data_flw$CHW, na.rm=T),2), HW=rep(mean(data_flw$HW, na.rm=T),2), female=rep(mean(data_flw$female, na.rm=T),2), white=rep(mean(data_flw$white, na.rm=T),2), age=rep(mean(data_flw$age, na.rm=T),2))
nd3



###########################
### PREDICT OUTCOME VALUES
###########################

### FEDERAL GOVERNMENT SUPPORT

pred.p1 = predict(logit_main_models_06, type="response", se.fit=TRUE, newdata=nd1)

fit.p1 = pred.p1
fit.p1 = pred.p1$fit
low.p1 = pred.p1$fit - 1.96*pred.p1$se.fit
high.p1 = pred.p1$fit + 1.96*pred.p1$se.fit
cis.p1 = cbind(fit.p1, low.p1, high.p1)

pred.p1.gg=as.data.frame(pred.p1)
cis.p1.gg=as.data.frame(cis.p1)

### STATE GOVERNMENT SUPPORT

pred.p2 = predict(logit_main_models_06, type="response", se.fit=TRUE, newdata=nd2)

fit.p2 = pred.p2
fit.p2 = pred.p2$fit
low.p2 = pred.p2$fit - 1.96*pred.p2$se.fit
high.p2 = pred.p2$fit + 1.96*pred.p2$se.fit
cis.p2 = cbind(fit.p2, low.p2, high.p2)

pred.p2.gg=as.data.frame(pred.p2)
cis.p2.gg=as.data.frame(cis.p2)

### LOCAL GOVERNMENT SUPPORT

pred.p3 = predict(logit_main_models_06, type="response", se.fit=TRUE, newdata=nd3)

fit.p3 = pred.p3
fit.p3 = pred.p3$fit
low.p3 = pred.p3$fit - 1.96*pred.p3$se.fit
high.p3 = pred.p3$fit + 1.96*pred.p3$se.fit
cis.p3 = cbind(fit.p3, low.p3, high.p3)

pred.p3.gg=as.data.frame(pred.p3)
cis.p3.gg=as.data.frame(cis.p3)



###########################
### CREATE INDIVIDUAL PLOTS
###########################

library(scales)

### FEDERAL GOVERNMENT SUPPORT

plot1=ggplot(pred.p1.gg, aes(x=c(0,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p1.gg$low.p1, ymax=cis.p1.gg$high.p1), alpha=0.1, fill="grey10", color="grey10", linetype = 2) + scale_fill_brewer(palette="Greys") + xlab("Federal Government Support") + ylab("Pr(Self-Efficacy = 1)") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 1))
plot1

### STATE GOVERNMENT SUPPORT

plot2=ggplot(pred.p2.gg, aes(x=c(0,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p2.gg$low.p2, ymax=cis.p2.gg$high.p2), alpha=0.1, fill="grey10", color="grey10", linetype = 2) + scale_fill_brewer(palette="Greys") + xlab("State Government Support") + ylab("Pr(Self-Efficacy = 1)") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 1))
plot2

### LOCAL GOVERNMENT SUPPORT

plot3=ggplot(pred.p3.gg, aes(x=c(0,1), y=fit)) +
  geom_ribbon(aes(ymin=cis.p3.gg$low.p3, ymax=cis.p3.gg$high.p3), alpha=0.1, fill="grey10", color="grey10", linetype = 2) + scale_fill_brewer(palette="Greys") + xlab("Local Government Support") + ylab("Pr(Self-Efficacy = 1)") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 1))
plot3



###########################
### PLOT WITH ALL ENTRIES (COMBINE INDIVIDUAL PLOTS INTO ONE)
### SELF-EFFICACY AND GOVERNMENT SUPPORT
### FIGURE 3 IN THE ARTICLE
###########################

library(gridExtra)

grid.arrange(plot1,plot2,plot3,nrow=2)



###########################
### CREATE FIGURE: SELF-EFFICACY AND OTHER INDEPENDENT VARIABLES
###########################

###########################
### NEW DATA FRAMES
###########################

### RESOURCES

nd4 = data.frame(fed_gov_support=rep(mean(data_flw$fed_gov_support, na.rm=T),4), state_gov_support=rep(mean(data_flw$state_gov_support, na.rm=T),4), local_gov_support=rep(mean(data_flw$local_gov_support, na.rm=T),4), resources=seq(0,1,by=1/3), managerial_support=rep(mean(data_flw$managerial_support, na.rm=T),4), demands=rep(mean(data_flw$demands, na.rm=T),4), CHW=rep(mean(data_flw$CHW, na.rm=T),4), HW=rep(mean(data_flw$HW, na.rm=T),4), female=rep(mean(data_flw$female, na.rm=T),4), white=rep(mean(data_flw$white, na.rm=T),4), age=rep(mean(data_flw$age, na.rm=T),4))
nd4

### MANAGERIAL SUPPORT

nd5 = data.frame(fed_gov_support=rep(mean(data_flw$fed_gov_support, na.rm=T),3), state_gov_support=rep(mean(data_flw$state_gov_support, na.rm=T),3), local_gov_support=rep(mean(data_flw$local_gov_support, na.rm=T),3), resources=rep(mean(data_flw$resources, na.rm=T),3), managerial_support=seq(0,1,by=0.5), demands=rep(mean(data_flw$demands, na.rm=T),3), CHW=rep(mean(data_flw$CHW, na.rm=T),3), HW=rep(mean(data_flw$HW, na.rm=T),3), female=rep(mean(data_flw$female, na.rm=T),3), white=rep(mean(data_flw$white, na.rm=T),3), age=rep(mean(data_flw$age, na.rm=T),3))
nd5

### DEMANDS

nd6 = data.frame(fed_gov_support=rep(mean(data_flw$fed_gov_support, na.rm=T),3), state_gov_support=rep(mean(data_flw$state_gov_support, na.rm=T),3), local_gov_support=rep(mean(data_flw$local_gov_support, na.rm=T),3), resources=rep(mean(data_flw$resources, na.rm=T),3), managerial_support=rep(mean(data_flw$managerial_support, na.rm=T),3), demands=seq(0,1,by=0.5), CHW=rep(mean(data_flw$CHW, na.rm=T),3), HW=rep(mean(data_flw$HW, na.rm=T),3), female=rep(mean(data_flw$female, na.rm=T),3), white=rep(mean(data_flw$white, na.rm=T),3), age=rep(mean(data_flw$age, na.rm=T),3))
nd6


###########################
### PREDICT OUTCOME VALUES
###########################

### RESOURCES

pred.p4 = predict(logit_main_models_06, type="response", se.fit=TRUE, newdata=nd4)

fit.p4 = pred.p4
fit.p4 = pred.p4$fit
low.p4 = pred.p4$fit - 1.96*pred.p4$se.fit
high.p4 = pred.p4$fit + 1.96*pred.p4$se.fit
cis.p4 = cbind(fit.p4, low.p4, high.p4)

pred.p4.gg=as.data.frame(pred.p4)
cis.p4.gg=as.data.frame(cis.p4)

### MANAGERIAL SUPPORT

pred.p5 = predict(logit_main_models_06, type="response", se.fit=TRUE, newdata=nd5)

fit.p5 = pred.p5
fit.p5 = pred.p5$fit
low.p5 = pred.p5$fit - 1.96*pred.p5$se.fit
high.p5 = pred.p5$fit + 1.96*pred.p5$se.fit
cis.p5 = cbind(fit.p5, low.p5, high.p5)

pred.p5.gg=as.data.frame(pred.p5)
cis.p5.gg=as.data.frame(cis.p5)

### DEMANDS

pred.p6 = predict(logit_main_models_06, type="response", se.fit=TRUE, newdata=nd6)

fit.p6 = pred.p6
fit.p6 = pred.p6$fit
low.p6 = pred.p6$fit - 1.96*pred.p6$se.fit
high.p6 = pred.p6$fit + 1.96*pred.p6$se.fit
cis.p6 = cbind(fit.p6, low.p6, high.p6)

pred.p6.gg=as.data.frame(pred.p6)
cis.p6.gg=as.data.frame(cis.p6)


###########################
### CREATE INDIVIDUAL PLOTS
###########################

### RESOURCES

plot4=ggplot(pred.p4.gg, aes(x=seq(0,1,by=1/3), y=fit)) +
  geom_ribbon(aes(ymin=cis.p4.gg$low.p4, ymax=cis.p4.gg$high.p4), alpha=0.1, fill="grey10", color="grey10", linetype = 2) + scale_fill_brewer(palette="Greys") + xlab("Resources") + ylab("Pr(Self-Efficacy = 1)") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5)) + scale_x_continuous(breaks=seq(0,1,by=1/3),labels=c("0","0.33","0.67","1"))
plot4

### MANAGERIAL SUPPORT

plot5=ggplot(pred.p5.gg, aes(x=seq(0,1,by=0.5), y=fit)) +
  geom_ribbon(aes(ymin=cis.p5.gg$low.p5, ymax=cis.p5.gg$high.p5), alpha=0.1, fill="grey10", color="grey10", linetype = 2) + scale_fill_brewer(palette="Greys") + xlab("Managerial Support") + ylab("Pr(Self-Efficacy = 1)") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5)) + scale_x_continuous(breaks=seq(0,1,by=0.5),labels=c("0","0.5","1"))
plot5

### DEMANDS

plot6=ggplot(pred.p6.gg, aes(x=seq(0,1,by=0.5), y=fit)) +
  geom_ribbon(aes(ymin=cis.p6.gg$low.p6, ymax=cis.p6.gg$high.p6), alpha=0.1, fill="grey10", color="grey10", linetype = 2) + scale_fill_brewer(palette="Greys") + xlab("Demands") + ylab("Pr(Self-Efficacy = 1)") + geom_line(size=2) + theme(axis.text=element_text(size=14,face="bold",color="Black"), axis.title=element_text(size=18,face="bold"), title=element_text(size=18,face="bold"), legend.text=element_text(size=14,face="bold",color="Black"), plot.title = element_text(hjust = 0.5)) + scale_x_continuous(breaks = scales::pretty_breaks(n = 2))
plot6



###########################
### PLOT WITH ALL ENTRIES (COMBINE INDIVIDUAL PLOTS INTO ONE)
### SELF-EFFICACY AND OTHER INDEPENDENT VARIABLES
### FIGURE 4 IN THE ARTICLE
###########################

grid.arrange(plot4,plot5,plot6,nrow=2)


